/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.dibu.tyut.myreflect.ex;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class MyReflect {

    public static void main(String[] args)
            throws ClassNotFoundException,
                    NoSuchFieldException,
                    IllegalAccessException,
                    NoSuchMethodException,
                    InvocationTargetException,
                    InstantiationException {
        Class<Dog> clazz = (Class<Dog>) Class.forName("com.neusoft.dibu.tyut.myreflect.ex.Dog");
        Dog dog = clazz.getConstructor().newInstance();

        Method method = clazz.getDeclaredMethod("strpublicMethod", int.class);
        //        invoke 返回方法的执行结果
        Object invoke = method.invoke(dog, 12345);

        System.out.println(invoke);

        //        dog.publicMethod();
        //        根据方法名获得有参的重载方法
        //        Method publicMethod = clazz.getDeclaredMethod("publicMethod", int.class);
        //        Object invoke = publicMethod.invoke(dog, 1);
        //        System.out.println(invoke);
        // 根据方法名获得无参的方法
        //        Method publicMethod = clazz.getDeclaredMethod("publicMethod");
        //        Object o = publicMethod.invoke(dog, 1);
        //        System.out.println(o);

        //        for (Method method : clazz.getDeclaredMethods()) {
        //            System.out.println(method);
        //        }

        System.out.println(clazz);
    }

    public static void main3(String[] args)
            throws ClassNotFoundException,
                    NoSuchFieldException,
                    IllegalAccessException,
                    NoSuchMethodException,
                    InvocationTargetException,
                    InstantiationException {
        Class<Cat> clazz = (Class<Cat>) Class.forName("com.neusoft.dibu.tyut.myreflect.ex.Cat");
        Cat cat = clazz.getConstructor().newInstance();
        Field publicField = clazz.getDeclaredField("publicField");
        Field privateField = clazz.getDeclaredField("privateField");

        System.out.println(privateField);
        //        授权访问
        privateField.setAccessible(true);
        privateField.set(cat, 100);
        System.out.println(privateField.get(cat));

        //        System.out.println(publicField);
        //        publicField.set(cat, 100);
        //        Object o = publicField.get(cat);
        //        System.out.println(o);

        //        for (Field declaredField : clazz.getDeclaredFields()) {
        //            System.out.println(declaredField);
        //        }

        //        for (Field field : clazz.getFields()) {
        //            System.out.println(field);
        //        }
    }

    public static void main1(String[] args)
            throws ClassNotFoundException,
                    NoSuchMethodException,
                    InvocationTargetException,
                    InstantiationException,
                    IllegalAccessException {
        Class<Cat> clazz = Cat.class;
        clazz = (Class<Cat>) new Cat().getClass();
        clazz = (Class<Cat>) Class.forName("com.neusoft.dibu.tyut.myreflect.ex.Cat");
        // Cat的类对象只能得到Cat的一个实例
        //        Cat cat = clazz.getConstructor().newInstance();
        //        Constructor<Cat> constructor = clazz.getConstructor();
        //        Cat cat = constructor.newInstance();
        //        这里是Spring框架的构造器注入的基础
        //        Cat cat = clazz.getConstructor(int.class).newInstance(10);
        Cat 小花 = clazz.getConstructor(int.class, String.class).newInstance(1, "小花");
        System.out.println(小花);
        //        Class[] interfaces = clazz.getInterfaces();
        //        Arrays.asList(interfaces).stream().forEach(System.out::println);

        //        System.out.println(clazz.getName());
        //        System.out.println(clazz.getSimpleName());
        //        System.out.println(clazz.isInterface());
        //        clazz = Class.forName("com.neusoft.dibu.tyut.myreflect.ex.CatService");
        //        System.out.println(clazz.getSimpleName());
        //        System.out.println(clazz.isInterface());
    }
}
